<template>
	<view class="shop-enterpage">
		<div class="text-area-box">
			<textarea style="width: 100%;" placeholder="请输入需求内容" v-model="formData.content" />
		</div>
		<view class="business-box">
			<view class="business-name">
				<span style="color: red;">*</span>联系人
			</view>
			<view class="flex-y-center inputbox">
				<input type="text" placeholder="请输入联系联系人" v-model="formData.real_name" />
			</view>
		</view>
		<view class="business-box">
			<view class="business-name">
				<span style="color: red;">*</span>联系电话
				<view class="flex-y-center  inputbox">
					<input type="text" placeholder="请输入联系电话"  v-model="formData.phone"/>
				</view>
			</view>
		</view>
		<view class="business-box">
			<view class="business-name">
				需求照片
			</view>
			<view class="flex-y-center">
				<uni-file-picker :image-styles="imageStyles1" v-model="formData.cover" @select="select1" @delete="coverhandleDelete" @fail="fail"></uni-file-picker>
			</view>
		</view>



		<div class="line"></div>
		<view class="btn-submit">
			<button type="default" size="small" hover-class="none" @click="submit()">提交发布</button>
		</view>
		<!-- <FootTabbar :currentIndex='2' /> -->
	</view>
</template>

<script>
	import FootTabbar from '@/components/foot-tabbar/foot-tabbar.vue'
	export default {
		components: {
			FootTabbar
		},
		data() {
			return {
				islogin: false,
				// 表单数据
				formData: {
					content:"",
					real_name : "",
					phone:"",
					cover : [],
				},
				imageStyles1: {
					width: 110,
					height: 110,
				},
				fileLists: [], //现场拍照
			}
		},
		onShow() {
			this.getdata();
		},
		methods: { 
			getdata() {
				this.$H.get('/user/info').then(res => {
					if (res.status == 200) {
						this.islogin = true;
					}
				})
			},
			// 照片列表上传的记录
			select1(e) {
				this.tempFilePaths = e.tempFilePaths
				this.upload(this.tempFilePaths)
			},

			upload(tempFilePaths) {
				if (this.tempFilePaths.length == 0) {
					return false
				}
				this.uploadFilePromise(tempFilePaths[0]);
			},
			
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://scny.nclavi.com/api/upload/image', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							"Access-Token": uni.getStorageSync('token'),
							"storeId": 10001,
							"platform": "h5"
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							let info = JSON.parse(res.data);
							if (info.status == 200) {
								this.tempFilePaths.splice(0, 1); //删除数组的第1个 因为已经提交好了
								
								this.formData.cover.push({
									"name":  info.data.fileInfo.file_name,
									"extname":  info.data.fileInfo.file_ext,
									"url":  info.data.fileInfo.external_url,
									'temp' : url,
								});
								
								this.upload(this.tempFilePaths)
							}
						}
					});
				})
			},
			coverhandleDelete(err) { // 删除图片
				const num = this.formData.cover.findIndex(v => v.temp === err.tempFilePath);
				this.formData.cover.splice(num, 1);
			},
			submit() {
				if (this.islogin == false) {
					uni.showToast({
						icon: "none",
						title: '请先登录'
					})
					return;
				}
				if (!this.formData.content) {
					uni.showToast({
						icon: 'error',
						title: '请输入发布内容'
					})
					return false
				}
				if (!this.formData.real_name) {
					uni.showToast({
						icon: 'error',
						title: '请输入联系人'
					})
					return false
				}
				if (!this.formData.phone) {
					uni.showToast({
						icon: 'error',
						title: '请输入联系电话'
					})
					return false
				}
				
				this.$H.post('/user/add_information', {
					form: this.formData
				}).then(res => {
					if (res.status == 500) {
						uni.showToast({
							icon: "none",
							title: res.message
						})
					}
					if (res.status == 200) {
						uni.showToast({
							icon: "success",
							title: res.message
						})

						setTimeout(() => {
							uni.switchTab({
								url: '/pages/my/my',
							})
						}, 2000)
					} 
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.text-area-box {
		background: #f1f1f1;
		padding: 10rpx;
		box-sizing: border-box;
		border-radius: 20rpx;
		margin-bottom: 10rpx;
	}

	.shop-enterpage {
		padding: 30rpx;
		background: #fff;
	}


	.business-box {
		color: #333;
		margin-bottom: 10rpx;
		.business-name {
			margin: 20rpx 0px;
		}
	}


	.line {
		height: 100px;
	}

	.btn-submit {
		position: fixed;
		bottom: 0px;
		left: 0px;
		width: 100%;

		button {
			background-color: #17bf8b;
			color: #fff;
			width: 690rpx;
			margin: 20rpx auto;
		}
	}


	
	.inputbox{
		border: 1px solid #f1f1f1;
		height: 70rpx;
		padding: 0px 10rpx;
		border-radius: 5rpx;
		margin-top: 20rpx;
		background: #f1f1f1;
	}
</style>